package com.neusoft.controller;

import com.neusoft.po.CommonResult;
import com.neusoft.po.User;
import com.neusoft.service.ClientWalletService;
import com.neusoft.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/UserController")
@RefreshScope //开启动态刷新
public class UserController {

	@Autowired
	private UserService userService;
	@Autowired
	private ClientWalletService clientWalletService;


	@GetMapping("/getUserByIdByPass/{userId}/{password}")
	public CommonResult getUserByIdByPass(@PathVariable("userId") String userId,
										  @PathVariable("password") String password) throws Exception {
		User param = new User();
		param.setUserId(userId);
		param.setPassword(password);
		User user = userService.getUserByIdByPass(param);
		Object result = user;
		return new CommonResult(200, "success", result);
	}

	@GetMapping("/getUserById/{userId}")
	public CommonResult getUserById(@PathVariable("userId") String userId) throws Exception {
		int result = userService.getUserById(userId);
		return new CommonResult(200, "success", result);
	}

	@PostMapping("/saveUser/{userId}/{password}/{userName}/{userSex}")
	public CommonResult<Integer> saveUser(@PathVariable("userId") String userId,
			@PathVariable("password") String password, @PathVariable("userName") String userName,
			@PathVariable("userSex") Integer userSex) throws Exception {
		User param = new User();
		param.setUserId(userId);
		param.setPassword(password);
		param.setUserName(userName);
		param.setUserSex(userSex);
		clientWalletService.initializeWallet(Long.parseLong(param.getUserId()));
		int result = userService.saveUser(param);
		return new CommonResult(200, "success", result);
	}

	@GetMapping("/getById/{userId}")
	public CommonResult getById(@PathVariable("userId") String userId) {
		User result=userService.getById(userId);
		return new CommonResult(200, "success", result);
	}

	@PostMapping("/updateUser")
	public CommonResult updateUser(@ModelAttribute User updateDto) {
		int result = userService.updateUser(
				updateDto.getUserId(),
				updateDto.getPassword(),
				updateDto.getUserName(),
				updateDto.getUserSex(),
				updateDto.getUserImg(),
				updateDto.getDelTag()
		);
		return new CommonResult(200, "success", result);
	}


}
